<?php
/**
 * Created by PhpStorm.
 * User: chentaohua
 * Date: 2019/1/5
 * Time: 17:52
 * 号码管理
 */

namespace app\admin\controller;

use app\admin\controller\Export;

class Mobile extends Common{

    /**
     * 列出所有号码
     */
    public function lst(){
        $is_excls = input('is_excls');
        $mobileModel = new \app\admin\model\MobileLib;
        $mobileObj = $mobileModel::with('member') -> paginate(config('common_page'));

        if(!empty($is_excls)){
            $this->export();exit;
        }
        
        $mobileArr = [];
        foreach ($mobileObj as $mobile){
            $mobileArr[] = $mobile -> toArray();
        }
        $this -> assign('page',$mobileObj -> render());
        $this -> assign('mobileArr',$mobileArr);
        return $this -> fetch();
    }

    /**
     * 商户导出excel
     */
    public function export()
    {

        $headArr = array("运营商","手机号码");
        $list = db("mobile_lib")->order("id desc")->select();
        $data=array();
        foreach($list as $k => $v){
            switch ($v['mobile_type']) {
                case '1':
                    $mobile_type = '电信';
                    break;
                case '2':
                    $mobile_type = '移动';
                    break;
                case '3';
                    $mobile_type = '联通';
                    break;
                default:
                    # code...
                    break;
            }
            $data[$k][] = $mobile_type;
            $data[$k][] = $v['mobile'];
        }
        $filename='电话列表-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data);
    }

    //发送记录
    public function send_recording(){
        $start_time = input('start_time');
        $end_time = input('end_time');
        $where = [];
        $pageParam = [];
        if(!empty($start_time) && !empty($end_time)){
            $start_time_string = strtotime($start_time);
            $end_time_string = strtotime($end_time);
            $where['send_time'] = ['between',[$start_time_string,$end_time_string]];
            $pageParam['query']['start_time'] = $start_time;
            $pageParam['query']['end_time'] = $end_time;
        }

        $is_excls = input('is_excls');
        $ids = input('ids');
        if(!empty($is_excls)){
            $id_array = explode(",", $ids);
            foreach ($id_array as $key => $value) {
                if(empty($value)){
                    unset($id_array[$key]);
                }
            }
            //对应导出
            if(!empty($id_array)){
                $where['id'] = ['in',$id_array];
            }
            $this->send_export($where);exit;
        }

        $list = db("sms_send")
            ->where($where)
            ->order("id desc")
            ->paginate(10,false,$pageParam)->each(function($item){
                $item['mobile_lib'] = json_decode($item['mobile_lib']);
                $item['mobile_lib_string'] = implode(",",$item['mobile_lib']);
                if(!empty($item['send_time'])){
                    $item['send_time'] = date('Y-m-d H:i:s',$item['send_time']);
                }else{
                    $item['send_time'] = '未发送！';
                }
                return $item;
            });

        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this -> fetch();
    }

    /**
     * 发送内容导出
     */
    public function send_export($where = [])
    {
        $headArr = array("发送内容","手机号码","发送时间");

        $list = db("sms_send")
            ->where($where)
            ->order("id desc")
            ->select();
        $data=[];
        foreach($list as $k => $v){
            if(!empty($v['send_time'])){
                $v['send_time'] = date('Y-m-d H:i:s',$v['send_time']);
            }else{
                $v['send_time'] = '未发送！';
            }
            $v['mobile_lib'] = json_decode($v['mobile_lib']);
            $v['mobile_lib_string'] = implode(",",$v['mobile_lib']);

            $data[$k][] = $v['content'];
            $data[$k][] = $v['mobile_lib_string'];
            $data[$k][] = $v['send_time'];
        }
        $filename='发送内容列表-'.date("Y-m-d H:i:s");
        (new Export())->ImportExcel($filename,$headArr,$data);
    }

    /**
    * 删除发送记录
    */
    public function del_send_recode(){
        $input = input();
        
        $string_ids = $input['string_ids'];

        $string_ids = explode("-", $string_ids);

        $where['id']=['in',$string_ids];

        db('sms_send')->where($where)->delete();
        $this->success('删除成功！');
    }
}